#include "renderer.h"
#include <cairo/cairo-ft.h>
#include <string.h>

// 辅助函数：绘制带高亮的文本
static void draw_highlighted_text(
    cairo_t* cr, 
    const char* text, 
    int x, int y, 
    FT_Face face, 
    const SearchResult* results
) {
    if (!text || !face) return;

    // 设置字体大小
    FT_Set_Pixel_Sizes(face, 0, 12);  // 12px高
    cairo_set_source_rgb(cr, 0, 0, 0); // 文本颜色：黑色

    int current_x = x;
    int current_pos = 0;
    const char* p = text;

    while (*p) {
        // 检查当前位置是否需要高亮
        bool highlight = false;
        if (results) {
            for (int i = 0; results[i].start_pos != -1; i++) {
                if (current_pos >= results[i].start_pos && 
                    current_pos < results[i].start_pos + results[i].length) {
                    highlight = true;
                    break;
                }
            }
        }

        // 绘制高亮背景
        if (highlight) {
            cairo_set_source_rgb(cr, 1, 1, 0); // 黄色背景
            cairo_rectangle(cr, current_x, y - 10, face->glyph->advance.x / 64, 14);
            cairo_fill(cr);
            cairo_set_source_rgb(cr, 0, 0, 0); // 恢复文本颜色
        }

        // 加载字符glyph
        FT_UInt glyph_index = FT_Get_Char_Index(face, (FT_ULong)*p);
        FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
        FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);

        // 创建Cairo字体表面并绘制
        cairo_surface_t* surface = cairo_image_surface_create_for_data(
            (unsigned char*)face->glyph->bitmap.buffer,
            CAIRO_FORMAT_A8,
            face->glyph->bitmap.width,
            face->glyph->bitmap.rows,
            face->glyph->bitmap.pitch
        );

        cairo_set_source_surface(cr, surface, 
            current_x + face->glyph->bitmap_left, 
            y - face->glyph->bitmap_top);
        cairo_paint(cr);
        cairo_surface_destroy(surface);

        // 更新位置
        current_x += face->glyph->advance.x / 64; // 转换为像素单位
        current_pos++;
        p++;

        // 自动换行
        if (current_x > 750) { // 页面宽度限制
            current_x = x;
            y += 20;
        }
    }
}

void render_page(
    cairo_t* cr, 
    int width, 
    int height, 
    const PageContent* page, 
    FT_Face default_face, 
    const SearchResult* search_results
) {
    // 白色背景
    cairo_set_source_rgb(cr, 1, 1, 1);
    cairo_paint(cr);

    // 绘制标题栏
    cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
    cairo_select_font_face(cr, "Arial", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
    cairo_set_font_size(cr, 16);
    cairo_move_to(cr, 20, 30);
    cairo_show_text(cr, "PDF阅读器 (带字体渲染)");

    // 绘制导航提示
    cairo_set_font_size(cr, 12);
    cairo_move_to(cr, 20, 55);
    cairo_show_text(cr, "按 左/右箭头 切换页面 | 按 / 输入搜索关键词 | ESC退出");

    if (!page || !page->content) {
        cairo_move_to(cr, 20, 80);
        cairo_show_text(cr, "无页面内容");
        return;
    }

    // 选择字体（优先使用页面字体，否则用默认字体）
    FT_Face used_face = default_face;
    if (page->font_count > 0) {
        FT_Face page_face = font_loader_load_face(&page->fonts[0]);
        if (page_face) {
            used_face = page_face;
        }
    }

    // 绘制带高亮的文本内容
    draw_highlighted_text(cr, page->content, 20, 80, used_face, search_results);

    // 释放临时字体face
    if (used_face != default_face) {
        FT_Done_Face(used_face);
    }
}